package com.huluobo.a2303mdmdemo.video.pages

import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Send
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.media3.common.MediaItem
import androidx.media3.common.Player.REPEAT_MODE_ONE
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2303mdmdemo.R
import com.huluobo.a2303mdmdemo.video.VideoIntent
import com.huluobo.a2303mdmdemo.video.VideoViewModel
import com.huluobo.a2303mdmdemo.video.entity.CommentEntity
import com.huluobo.a2303mdmdemo.video.entity.CommentEntityItem
import com.huluobo.lib_base.room.VideoEntity
import com.huluobo.lib_base.room.VideoEntityItem
import com.huluobo.lib_base.StateType
import com.huluobo.lib_base.UIState
import com.huluobo.lib_http.Const
import com.umeng.socialize.ShareAction
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/8/29.
 */

@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
@Composable
fun VideoPagerPage(navHostController: NavHostController, vm: VideoViewModel = hiltViewModel()) {
    //视频的数据源
    val list = remember {
        SnapshotStateList<VideoEntityItem>()
    }
    //上下文
    val context = LocalContext.current

    //纵向滑动列表的状态
    val pagerState = rememberPagerState {
        list.size
    }

    //优化版的状态 通过列表状态取出的当前的页面下标
    val currentPage by remember {
        derivedStateOf { pagerState.currentPage }
    }

    //播放器的集合
    val players = remember {
        mutableListOf<ExoPlayer>()
    }

    //点赞的状态
    var isLike by remember {
        mutableStateOf(false)
    }

    //是否弹出评论框
    var isShowComment by remember {
        mutableStateOf(false)
    }

    //评论的数据源
    val commentList = remember {
        SnapshotStateList<CommentEntityItem>()
    }

    //评论内容
    var commentString by remember {
        mutableStateOf("")
    }

    //当下标改变的时候获取的线程
    LaunchedEffect(currentPage) {
        players.forEachIndexed { index, exoPlayer ->
            //如果当前页面的下标等于循环中的下标,证明当前展示的播放器可以播放,其他的播放器暂停
            if (index == currentPage) exoPlayer.play() else exoPlayer.pause()
        }
    }

    //初始化
    DisposableEffect(list.size) {
        //重复列表的次数
        repeat(list.size) {
            //给每一个播放器添加播放源
            players.add(createPlayer(context).apply {
                setMediaItem(MediaItem.fromUri(list[it].videopath))
                prepare()
            })
        }
        onDispose {
            //销毁时,每个播放器都需要被销毁
            players.forEach { it.release() }
        }
    }

    //初始化请求数据的线程
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnFailed -> {
                        ToastUtils.showLong("获取视频列表失败:${it.msg}")
                        Log.e("TagA", "获取视频列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {
                    }

                    is UIState.OnSuccess<*> -> {
                        if (it.type == StateType.DEFAULT) {
                            list.clear()
                            list.addAll(it.data as VideoEntity)
                        }

                        if (it.type == StateType.LIKE) {
                            list[currentPage].dianzan = 1
                            ToastUtils.showLong("点赞成功")
                            vm.sendIntent(VideoIntent.GetAllVideos(3, 10))
                        }

                        if (it.type == StateType.NO_LIKE) {
                            list[currentPage].dianzan = 0
                            ToastUtils.showLong("取消点赞成功")
                            vm.sendIntent(VideoIntent.GetAllVideos(3, 10))
                        }

                        if (it.type == StateType.GET_COMMENT) {
                            commentList.clear()
                            commentList.addAll(it.data as CommentEntity)
                        }

                        if (it.type == StateType.SEND_COMMENT) {
                            commentList.addAll(0, it.data as CommentEntity)
                            ToastUtils.showLong("发表评论成功")
                            commentString = ""
                        }

                        if (it.type == StateType.COLLECT) {
                            ToastUtils.showLong("收藏成功")
                        }

                        if (it.type == StateType.FOLLOW_AUTH) {
                            ToastUtils.showLong("关注成功")
                            vm.sendIntent(VideoIntent.GetAllVideos(3, 10))
                        }
                    }
                }
            }
        }
        vm.sendIntent(VideoIntent.GetAllVideos(3, 10))
    })

    VerticalPager(state = pagerState, modifier = Modifier.fillMaxSize()) { index ->
        when (list[index].type) {
            0 -> {
                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomEnd) {
                    //每一个页面
                    AndroidView(factory = {
                        PlayerView(it).apply {
                            player = players[index]
                            useController = true
                        }
                    }, modifier = Modifier.fillMaxSize())

                    //用户交互的UI
                    Column(
                        modifier = Modifier
                            .padding(20.dp)
                            .padding(bottom = 100.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Box(contentAlignment = Alignment.BottomCenter) {
                            AsyncImage(
                                model = list[index].headpath, contentDescription = "", modifier = Modifier
                                    .size(40.dp)
                                    .clip(
                                        CircleShape
                                    )
                            )
                            if (list[index].guanzhu == 0) {
                                Icon(
                                    imageVector = Icons.Default.Add, contentDescription = "",
                                    modifier = Modifier
                                        .offset(y = 10.dp)
                                        .size(20.dp)
                                        .background(
                                            Color.Red,
                                            CircleShape
                                        )
                                        .clickable {
                                            val map = mapOf(
                                                Const.PARAM_AUTHNAME to list[index].authname
                                            )
                                            vm.sendIntent(VideoIntent.FollowAuth(map))
                                        },
                                    tint = Color.White,
                                )
                            }
                        }
                        Spacer(modifier = Modifier.height(10.dp))

                        IconButton(onClick = {
                            isLike = list[index].dianzan == 1
                            if (isLike) {
                                //true 证明已点赞,需要发送取消点赞意图
                                vm.sendIntent(VideoIntent.NoLikeVideo(list[index].id))
                            } else {
                                //fasle 证明未点赞,需要发送点赞意图
                                vm.sendIntent(VideoIntent.LikeVideo(list[index].id))
                            }
                        }) {
                            Icon(
                                painter = painterResource(id = if (list[index].dianzan == 1) R.drawable.like else R.drawable.un_like),
                                contentDescription = "",
                                modifier = Modifier.size(40.dp),
                                tint = if (list[index].dianzan == 1) Color.Red else Color.White
                            )
//                    Image(
//                        painter = painterResource(id = if (isLike) R.drawable.like else R.drawable.un_like),
//                        contentDescription ="",
//                        modifier = Modifier.size(40.dp)
//                    )
                        }
                        Text(text = "点赞", color = Color.White)

                        IconButton(onClick = {
                            //展示评论框
                            isShowComment = true
                        }) {
                            Image(
                                painter = painterResource(id = R.drawable.baseline_chat_24),
                                contentDescription = "",
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        Text(text = "评论", color = Color.White)

                        IconButton(onClick = {
                            vm.sendIntent(VideoIntent.CollectVideo(list[index]))
                        }) {
                            Icon(
                                imageVector = Icons.Default.Star,
                                contentDescription = "",
                                tint = Color.Yellow,
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        Text(text = "收藏", color = Color.White)

                        IconButton(onClick = {
                            val umImage = UMImage(context, list[index].videomainimg)
                            umImage.compressStyle = UMImage.CompressStyle.SCALE

                            val umWeb = UMWeb(list[index].videopath)
                            umWeb.title = list[index].authname
                            umWeb.setThumb(umImage)
                            umWeb.description = list[index].caption

                            ShareAction(context as Activity)
                                .withMedia(umWeb)
                                .open()
                        }) {
                            Icon(
                                imageVector = Icons.Default.Share,
                                contentDescription = "",
                                tint = Color.White,
                                modifier = Modifier.size(40.dp)
                            )
                        }

                        Text(text = "分享", color = Color.White)
                    }

                    if (isShowComment) {
                        vm.sendIntent(VideoIntent.GetAllComment(list[index].id))
                        Scaffold(modifier = Modifier
                            .height(300.dp)
                            .clip(RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                            bottomBar = {
                                OutlinedTextField(
                                    value = commentString,
                                    onValueChange = {
                                        commentString = it
                                    },
                                    modifier = Modifier.fillMaxWidth(),
                                    trailingIcon = {
                                        Icon(
                                            imageVector = Icons.Default.Send,
                                            contentDescription = "",
                                            modifier = Modifier.clickable {
                                                val map = mapOf(
                                                    Const.PARAM_VIDEO_ID to list[index].id,
                                                    Const.PARAM_MSG to commentString
                                                )
                                                vm.sendIntent(VideoIntent.SendComment(map))
                                            })
                                    })
                            }
                        ) {
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .padding(it)
                                    .height(300.dp)
                                    .background(Color.White, RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                                contentAlignment = Alignment.TopEnd
                            ) {
                                LazyColumn(content = {
                                    items(commentList) {
                                        CommentItem(entityItem = it)
                                    }
                                }, modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(5.dp))

                                Icon(
                                    imageVector = Icons.Default.Close,
                                    contentDescription = "",
                                    modifier = Modifier
                                        .size(30.dp)
                                        .clickable {
                                            isShowComment = false
                                        }
                                        .padding(end = 10.dp, top = 10.dp)
                                )
                            }
                        }

                    }
                }
            }

            1 -> {
                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomEnd) {
                    //每一个页面
                    AndroidView(factory = {
                        PlayerView(it).apply {
                            player = players[index]
                            useController = true
                        }
                    }, modifier = Modifier.fillMaxSize())

                    //用户交互的UI
                    Column(
                        modifier = Modifier
                            .padding(20.dp)
                            .padding(bottom = 100.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        IconButton(onClick = {
                            isLike = list[index].dianzan == 1
                            if (isLike) {
                                //true 证明已点赞,需要发送取消点赞意图
                                vm.sendIntent(VideoIntent.NoLikeVideo(list[index].id))
                            } else {
                                //fasle 证明未点赞,需要发送点赞意图
                                vm.sendIntent(VideoIntent.LikeVideo(list[index].id))
                            }
                        }) {
                            Icon(
                                painter = painterResource(id = if (list[index].dianzan == 1) R.drawable.like else R.drawable.un_like),
                                contentDescription = "",
                                modifier = Modifier.size(40.dp),
                                tint = if (list[index].dianzan == 1) Color.Red else Color.White
                            )
//                    Image(
//                        painter = painterResource(id = if (isLike) R.drawable.like else R.drawable.un_like),
//                        contentDescription ="",
//                        modifier = Modifier.size(40.dp)
//                    )
                        }
                        Text(text = "点赞", color = Color.White)

                        IconButton(onClick = {
                            //展示评论框
                            isShowComment = true
                        }) {
                            Image(
                                painter = painterResource(id = R.drawable.baseline_chat_24),
                                contentDescription = "",
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        Text(text = "评论", color = Color.White)

                        IconButton(onClick = {
                            vm.sendIntent(VideoIntent.CollectVideo(list[index]))
                        }) {
                            Icon(
                                imageVector = Icons.Default.Star,
                                contentDescription = "",
                                tint = Color.Yellow,
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        Text(text = "收藏", color = Color.White)

                        IconButton(onClick = {
                            val umImage = UMImage(context, list[index].videomainimg)
                            umImage.compressStyle = UMImage.CompressStyle.SCALE

                            val umWeb = UMWeb(list[index].videopath)
                            umWeb.title = list[index].authname
                            umWeb.setThumb(umImage)
                            umWeb.description = list[index].caption

                            ShareAction(context as Activity)
                                .withMedia(umWeb)
                                .open()
                        }) {
                            Icon(
                                imageVector = Icons.Default.Share,
                                contentDescription = "",
                                tint = Color.White,
                                modifier = Modifier.size(40.dp)
                            )
                        }

                        Text(text = "分享", color = Color.White)
                    }

                    if (isShowComment) {
                        vm.sendIntent(VideoIntent.GetAllComment(list[index].id))
                        Scaffold(modifier = Modifier
                            .height(300.dp)
                            .clip(RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                            bottomBar = {
                                OutlinedTextField(
                                    value = commentString,
                                    onValueChange = {
                                        commentString = it
                                    },
                                    modifier = Modifier.fillMaxWidth(),
                                    trailingIcon = {
                                        Icon(
                                            imageVector = Icons.Default.Send,
                                            contentDescription = "",
                                            modifier = Modifier.clickable {
                                                val map = mapOf(
                                                    Const.PARAM_VIDEO_ID to list[index].id,
                                                    Const.PARAM_MSG to commentString
                                                )
                                                vm.sendIntent(VideoIntent.SendComment(map))
                                            })
                                    })
                            }
                        ) {
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .padding(it)
                                    .height(300.dp)
                                    .background(Color.White, RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                                contentAlignment = Alignment.TopEnd
                            ) {
                                LazyColumn(content = {
                                    items(commentList) {
                                        CommentItem(entityItem = it)
                                    }
                                }, modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(5.dp))

                                Icon(
                                    imageVector = Icons.Default.Close,
                                    contentDescription = "",
                                    modifier = Modifier
                                        .size(30.dp)
                                        .clickable {
                                            isShowComment = false
                                        }
                                        .padding(end = 10.dp, top = 10.dp)
                                )
                            }
                        }

                    }
                }

            }

            2 -> {
                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomEnd) {
                    //每一个页面
                    AndroidView(factory = {
                        PlayerView(it).apply {
                            player = players[index]
                            useController = true
                        }
                    }, modifier = Modifier.fillMaxSize())

                    //用户交互的UI
                    Column(
                        modifier = Modifier
                            .padding(20.dp)
                            .padding(bottom = 100.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Box(contentAlignment = Alignment.BottomCenter) {
                            AsyncImage(
                                model = list[index].headpath, contentDescription = "", modifier = Modifier
                                    .size(40.dp)
                                    .clip(
                                        CircleShape
                                    )
                            )
                            if (list[index].guanzhu == 0) {
                                Icon(
                                    imageVector = Icons.Default.Add, contentDescription = "",
                                    modifier = Modifier
                                        .offset(y = 10.dp)
                                        .size(20.dp)
                                        .background(
                                            Color.Red,
                                            CircleShape
                                        )
                                        .clickable {
                                            val map = mapOf(
                                                Const.PARAM_AUTHNAME to list[index].authname
                                            )
                                            vm.sendIntent(VideoIntent.FollowAuth(map))
                                        },
                                    tint = Color.White,
                                )
                            }
                        }
                        Spacer(modifier = Modifier.height(10.dp))

                        IconButton(onClick = {
                            //展示评论框
                            isShowComment = true
                        }) {
                            Image(
                                painter = painterResource(id = R.drawable.baseline_chat_24),
                                contentDescription = "",
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        Text(text = "评论", color = Color.White)

                        IconButton(onClick = {
                            vm.sendIntent(VideoIntent.CollectVideo(list[index]))
                        }) {
                            Icon(
                                imageVector = Icons.Default.Star,
                                contentDescription = "",
                                tint = Color.Yellow,
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        Text(text = "收藏", color = Color.White)

                        IconButton(onClick = {
                            val umImage = UMImage(context, list[index].videomainimg)
                            umImage.compressStyle = UMImage.CompressStyle.SCALE

                            val umWeb = UMWeb(list[index].videopath)
                            umWeb.title = list[index].authname
                            umWeb.setThumb(umImage)
                            umWeb.description = list[index].caption

                            ShareAction(context as Activity)
                                .withMedia(umWeb)
                                .open()
                        }) {
                            Icon(
                                imageVector = Icons.Default.Share,
                                contentDescription = "",
                                tint = Color.White,
                                modifier = Modifier.size(40.dp)
                            )
                        }

                        Text(text = "分享", color = Color.White)
                    }

                    if (isShowComment) {
                        vm.sendIntent(VideoIntent.GetAllComment(list[index].id))
                        Scaffold(modifier = Modifier
                            .height(300.dp)
                            .clip(RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                            bottomBar = {
                                OutlinedTextField(
                                    value = commentString,
                                    onValueChange = {
                                        commentString = it
                                    },
                                    modifier = Modifier.fillMaxWidth(),
                                    trailingIcon = {
                                        Icon(
                                            imageVector = Icons.Default.Send,
                                            contentDescription = "",
                                            modifier = Modifier.clickable {
                                                val map = mapOf(
                                                    Const.PARAM_VIDEO_ID to list[index].id,
                                                    Const.PARAM_MSG to commentString
                                                )
                                                vm.sendIntent(VideoIntent.SendComment(map))
                                            })
                                    })
                            }
                        ) {
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .padding(it)
                                    .height(300.dp)
                                    .background(Color.White, RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                                contentAlignment = Alignment.TopEnd
                            ) {
                                LazyColumn(content = {
                                    items(commentList) {
                                        CommentItem(entityItem = it)
                                    }
                                }, modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(5.dp))

                                Icon(
                                    imageVector = Icons.Default.Close,
                                    contentDescription = "",
                                    modifier = Modifier
                                        .size(30.dp)
                                        .clickable {
                                            isShowComment = false
                                        }
                                        .padding(end = 10.dp, top = 10.dp)
                                )
                            }
                        }

                    }
                }
            }
        }

        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomEnd) {
            //每一个页面
            AndroidView(factory = {
                PlayerView(it).apply {
                    player = players[index]
                    useController = true
                }
            }, modifier = Modifier.fillMaxSize())

            //用户交互的UI
            Column(
                modifier = Modifier
                    .padding(20.dp)
                    .padding(bottom = 100.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Box(contentAlignment = Alignment.BottomCenter) {
                    AsyncImage(
                        model = list[index].headpath, contentDescription = "", modifier = Modifier
                            .size(40.dp)
                            .clip(
                                CircleShape
                            )
                    )
                    if (list[index].guanzhu == 0) {
                        Icon(
                            imageVector = Icons.Default.Add, contentDescription = "",
                            modifier = Modifier
                                .offset(y = 10.dp)
                                .size(20.dp)
                                .background(
                                    Color.Red,
                                    CircleShape
                                )
                                .clickable {
                                    val map = mapOf(
                                        Const.PARAM_AUTHNAME to list[index].authname
                                    )
                                    vm.sendIntent(VideoIntent.FollowAuth(map))
                                },
                            tint = Color.White,
                        )
                    }
                }
                Spacer(modifier = Modifier.height(10.dp))

                IconButton(onClick = {
                    isLike = list[index].dianzan == 1
                    if (isLike) {
                        //true 证明已点赞,需要发送取消点赞意图
                        vm.sendIntent(VideoIntent.NoLikeVideo(list[index].id))
                    } else {
                        //fasle 证明未点赞,需要发送点赞意图
                        vm.sendIntent(VideoIntent.LikeVideo(list[index].id))
                    }
                }) {
                    Icon(
                        painter = painterResource(id = if (list[index].dianzan == 1) R.drawable.like else R.drawable.un_like),
                        contentDescription = "",
                        modifier = Modifier.size(40.dp),
                        tint = if (list[index].dianzan == 1) Color.Red else Color.White
                    )
//                    Image(
//                        painter = painterResource(id = if (isLike) R.drawable.like else R.drawable.un_like),
//                        contentDescription ="",
//                        modifier = Modifier.size(40.dp)
//                    )
                }
                Text(text = "点赞", color = Color.White)

                IconButton(onClick = {
                    //展示评论框
                    isShowComment = true
                }) {
                    Image(
                        painter = painterResource(id = R.drawable.baseline_chat_24),
                        contentDescription = "",
                        modifier = Modifier.size(40.dp)
                    )
                }
                Text(text = "评论", color = Color.White)

                IconButton(onClick = {
                    vm.sendIntent(VideoIntent.CollectVideo(list[index]))
                }) {
                    Icon(
                        imageVector = Icons.Default.Star,
                        contentDescription = "",
                        tint = Color.Yellow,
                        modifier = Modifier.size(40.dp)
                    )
                }
                Text(text = "收藏", color = Color.White)

                IconButton(onClick = {
                    val umImage = UMImage(context, list[index].videomainimg)
                    umImage.compressStyle = UMImage.CompressStyle.SCALE

                    val umWeb = UMWeb(list[index].videopath)
                    umWeb.title = list[index].authname
                    umWeb.setThumb(umImage)
                    umWeb.description = list[index].caption

                    ShareAction(context as Activity)
                        .withMedia(umWeb)
                        .open()
                }) {
                    Icon(
                        imageVector = Icons.Default.Share,
                        contentDescription = "",
                        tint = Color.White,
                        modifier = Modifier.size(40.dp)
                    )
                }

                Text(text = "分享", color = Color.White)
            }

            if (isShowComment) {
                vm.sendIntent(VideoIntent.GetAllComment(list[index].id))
                Scaffold(modifier = Modifier
                    .height(300.dp)
                    .clip(RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                    bottomBar = {
                        OutlinedTextField(
                            value = commentString,
                            onValueChange = {
                                commentString = it
                            },
                            modifier = Modifier.fillMaxWidth(),
                            trailingIcon = {
                                Icon(
                                    imageVector = Icons.Default.Send,
                                    contentDescription = "",
                                    modifier = Modifier.clickable {
                                        val map = mapOf(
                                            Const.PARAM_VIDEO_ID to list[index].id,
                                            Const.PARAM_MSG to commentString
                                        )
                                        vm.sendIntent(VideoIntent.SendComment(map))
                                    })
                            })
                    }
                ) {
                    Box(
                        Modifier
                            .fillMaxWidth()
                            .padding(it)
                            .height(300.dp)
                            .background(Color.White, RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                        contentAlignment = Alignment.TopEnd
                    ) {
                        LazyColumn(content = {
                            items(commentList) {
                                CommentItem(entityItem = it)
                            }
                        }, modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(5.dp))

                        Icon(
                            imageVector = Icons.Default.Close,
                            contentDescription = "",
                            modifier = Modifier
                                .size(30.dp)
                                .clickable {
                                    isShowComment = false
                                }
                                .padding(end = 10.dp, top = 10.dp)
                        )
                    }
                }

            }
        }
    }
}

private fun createPlayer(context: Context): ExoPlayer {
    return ExoPlayer.Builder(context).build().apply {
        repeatMode = REPEAT_MODE_ONE
//        playWhenReady = true
    }
}

@Composable
fun CommentItem(entityItem: CommentEntityItem) {
    Column(
        modifier = Modifier
            .padding(5.dp)
            .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(5.dp)
    ) {
        Row {
            AsyncImage(
                model = entityItem.icon, contentDescription = "", modifier = Modifier
                    .size(40.dp)
                    .clip(
                        CircleShape
                    ), contentScale = ContentScale.Crop
            )
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "${entityItem.username}")
        }
        Text(text = "${entityItem.msg}")
        Text(text = "${entityItem.createtime}", fontSize = 12.sp, color = Color.LightGray)
    }
}